package com.ddwl.message.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.message.service.MessageNoticeService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.gate.MessageInfoBo;
import com.ddwl.schema.bo.gate.MessageInfoCreateBo;
import com.ddwl.schema.bo.gate.MessageUserBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.gate.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/messageNotice")
@Api(tags = "站内信消息服务")
public class MessageNoticeController extends BaseController {

    @Autowired
    public MessageNoticeService messageNoticeService;

    @PostMapping("/ConfList")
    @ApiOperation("获取消息配置列表")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<PageVo<MessageInfoVo>> getConfList(@RequestBody PageBo<MessageInfoBo> pageBo){
        return RestVo.ok(messageNoticeService.getList(pageBo));
    }

    @PostMapping("/create")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    @ApiOperation("创建消息模板")
    public RestVo createMessage(@Valid @RequestBody MessageInfoCreateBo pageBo){
        messageNoticeService.createMessage(pageBo);
        return RestVo.ok();
    }

    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    @GetMapping("/detail")
    @ApiOperation("查看详情")
    public RestVo<MessageInfoVo> detail(@RequestParam(name = "id") String id){
        return RestVo.ok(messageNoticeService.detail(id));
    }

    @DeleteMapping("del/{messageInfoId}")
    @ApiOperation("删除消息模板")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo delete(@PathVariable("messageInfoId")String messageInfoId){
        messageNoticeService.delete(messageInfoId);
        return RestVo.ok();
    }

    @PostMapping("/getMessage")
    @ApiOperation("查看当前用户消息列表")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.MALL})
    public RestVo<PageVo<MessageUserListVo>> getMessageList(@RequestBody PageBo<MessageUserBo> pageBo){
        pageBo.getParam().setUserId(getUserId());
       return RestVo.ok(messageNoticeService.messageList(pageBo));
    }

    @PostMapping("getMessageDetail")
    @ApiOperation("查看当前用户消息详情")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.MALL})
    public RestVo<MessageInfoDetailVo> getMessageDetail(@RequestBody MessageUserBo messageUserBo){
        messageUserBo.setUserId(getUserId());
        return RestVo.ok(messageNoticeService.getMessageDetail(messageUserBo));
    }

    @PostMapping("getDataList")
    @ApiOperation("查看数据列表")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo<PageVo<MessageDataVo>> getDataList(@RequestBody PageBo<MessageUserBo> pageBo){
        return RestVo.ok(messageNoticeService.getDataList(pageBo));
    }

    @GetMapping("getHasReaded")
    @ApiOperation("判断用户是否有无未读消息")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.MALL})
    public RestVo<MessageIsReadVo> getHasReaded(){
        return RestVo.ok(messageNoticeService.hasUnRead(getUserId()));
    }

    @PostMapping("/changeId")
    @ApiOperation("把id反转成时间戳")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo changeId(){
        messageNoticeService.changeId();
        return RestVo.ok();
    }

}